package com.shijichaoxing.service.impl;

import com.shijichaoxing.dao.AdminDao;
import com.shijichaoxing.entity.Admin;
import com.shijichaoxing.service.AdminService;
import com.shijichaoxing.utils.PageParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zj
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminDao adminDao;

    @Override
    public boolean login(Admin admin) {
        return adminDao.login(admin).size() == 1 ? true : false;
    }

    @Override
    public boolean addAdmin(Admin admin) {
        return adminDao.addAdmin(admin);
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        return adminDao.updateAdmin(admin);
    }

    @Override
    public Admin findAdminById(Integer id) {
        return adminDao.findAdminById(id);
    }

    @Override
    public void findAdminByPage(PageParam<Admin> pageParam, String indexName, boolean isAsc) {
        int start = (pageParam.getCurrPage() - 1) * pageParam.getPageSize();
        indexName = switchInfo(indexName);
        pageParam.setData(adminDao.findAdminByPage(start, pageParam.getPageSize(), indexName, isAsc));
        pageParam.setRowCount(adminDao.countAll());
    }

    @Override
    public boolean deleteById(Integer id) {
        return adminDao.deleteById(id);
    }

    private String switchInfo(String indexName) {
        switch (indexName) {
            default:
                return "id";
            case "admin_id":
            case "adminId":
            case "id":
                return "id";
            case "realName":
                return "real_name";
            case "password":
                return "password";
            case "createTime":
                return "create_time";
            case "updateTime":
                return "update_time";
        }
    }
}